< previous page page_380 next page >

Page 380
The next section discusses sequential file management. Because much of the same pattern of operation for sequential files is similar to that of random files, the discussion is briefer.
Sequential File Management
Sequential file management is characterized by the following operations:
Assign characters from a file to some variable via the Input statement
Persist characters to a file from some variable via the Output statement
Persist additional characters to the end of an existing file from some variable via the Append statement
Opening a Sequential Access File
Opening a file for random access is also pretty easy. The following syntax is required:
Open pathname For [Input | Output | Append] As filenumber [Len = buffersize]
The Input keyword works only with files that already exist. Using the keyword otherwise results in an error, so it is good to have a class or class method that is responsible for making sure the file indeed exists and to raise an error or event in case it doesn't. Opening a nonexistent file using the Output keyword simply results in a new file. You might even have a class method name, createNewFile(argFileName), with the sole purpose of opening a file for Output using the value of the filename in the argFileName argument. Although the Append keyword has the same effect as the Output statement, your code would be more readable and logical if you used this keyword only for existing files. You could have a method named persistAddedInfo(argFileName) or appendInfo(argFileName) for this purpose.
The optional Len argument indicates the number of characters to buffer when copying information between the file and your application. Use the Close statement to close the file.
Modifying Sequential Access Files
Modifying sequential access files follows the same pattern as modifying random access files. Once you open a file for sequential access, use either the Line Input #, Input( ), or Input # statement to map values in the file to your variables. A feature of sequential files is that you can read information contained in them either one character at a time or an entire line at a time. Listing 17.2 shows some code that reads a file one line at a time.

 
< previous page page_380 next page >

If you like this book, buy it!